function ClearWindow (ds, source, property, target)
{
	var rdf = CC["@mozilla.org/rdf/rdf-service;1"].getService(CI.nsIRDFService);
	var thread = source.Value.substr(0, source.Value.lastIndexOf("/"));
	if (thread != "urn:xmpp:message:"+window.name) return;
	var msg = document.getElementById(source.Value);
	if (msg)
		document.removeChild(msg);
}

function AddMessage (ds, source, property, target)
{
	var rdf = CC["@mozilla.org/rdf/rdf-service;1"].getService(CI.nsIRDFService);
	var thread = source.Value.substr(0, source.Value.lastIndexOf("/"));
	if(thread!= "urn:xmpp:message:" + window.name)
		return;
	if (property.Value != "http://xmpp.org/RDF#msg")
							return;
	var frame = document.getElementById("msgframe");
	if (frame) 
	{
	    var box = frame.contentDocument.getElementById("output-box");
	    var msg = getDiv(target.QueryInterface(CI.nsIRDFLiteral).Value);
	    if (box)
		{
		    box.appendChild(render.paint(source));
		    frame.contentWindow.scrollTo(0, frame.contentDocument.height);
			setFocus();
	    }
	}
}

Jazzila.prototype.clearConversation = function ()
{
	var rdf = CC["@mozilla.org/rdf/rdf-service;1"].getService(CI.nsIRDFService);
	var rdfCU = CC["@mozilla.org/rdf/container-utils;1"].getService(CI.nsIRDFContainerUtils); 
	var urn = rdf.GetResource("urn:xmpp:chat:"+window.name);
	var ctn = rdfCU.MakeSeq(ds, urn);
	var etor = ctn.GetElements();
	while (etor.hasMoreElements())
	{
		ctn.RemoveElement(etor.getNext().QueryInterface(CI.nsIRDFNode), false);
	}
	var frame = document.getElementById("output-table");
	if (frame)
	{
		frame.contentWindow.name = window.name;
		frame.webNavigation.reload(0);
	}

}

Jazzila.prototype.saveConversation = function ()
{
	var thread = window.name;
	var fp = CC["@mozilla.org/filepicker;1"].createInstance(CI.nsIFilePicker);
	fp.init(window, "Select a File", CI.nsIFilePicker.modeSave);
	fp.appendFilters(CI.nsIFilePicker.filterText | CI.nsIFilePicker.filterAll);
	fp.appendFilter("log files", "*.log");
	fp.defaultExtension = "log";
	var dt = new Date();
	fp.defaultString = thread.substr(0, thread.indexOf("@")) + "-"+ getDate();
	var res = fp.show();

	if (res != CI.nsIFilePicker.returnOK && res!=CI.nsIFilePicker.returnReplace){
		return;
	}

	this.writeLog(fp.file)
}		

Jazzila.prototype.writeLog = function (file)
{
	var thread = window.name;
	var out = CC["@mozilla.org/network/file-output-stream;1"].createInstance(CI.nsIFileOutputStream);
	out.init(file, 0x04 | 0x08 | 0x20, 420, 0 );
	var rdf = CC["@mozilla.org/rdf/rdf-service;1"].getService(CI.nsIRDFService);
	var rdfCU = CC["@mozilla.org/rdf/container-utils;1"].getService(CI.nsIRDFContainerUtils); 
	var urn = rdf.GetResource("urn:xmpp:chat:"+thread);
	var ctn = rdfCU.MakeSeq(ds, urn);
	var etor = ctn.GetElements();
	var rsTimestamp = rdf.GetResource("http://xmpp.org/RDF#timestamp");
	var rsJid = rdf.GetResource("http://xmpp.org/RDF#jid");
	var rsMsg = rdf.GetResource("http://xmpp.org/RDF#msg");
	var rsStyle = rdf.GetResource("http://xmpp.org/RDF#style");
	while (etor.hasMoreElements())
	{
		var elem = etor.getNext().QueryInterface(CI.nsIRDFResource);
		var jid = "<"+getRDFLiteral(ds, elem, rsJid)+">";
		var style = getRDFLiteral(ds, elem, rsStyle);
		var msg = getRDFLiteral(ds, elem, rsMsg);
		var tm = getTime(getRDFLiteral(ds, elem, rsTimestamp));
		var ln= tm + " "+jid + msg +"\n";
		out.write(ln, ln.length);
	}
	out.close();
}

Jazzila.prototype.onClick = function (event)
{
    switch (event.button)
    {
	case 2:
	    this.showContextMenu(event.clientX, event.clientY, "chat-context-menu", "output");		
	    break;
    };
}

Jazzila.prototype.sendMessage = function (e)
{
    var thread = window.name;
    var jid = thread;
    this.nsIXmppService.chat(jid, null, e.line, thread);
    e.line.value = "";
}

function onInputKeyPress (e)
{
    switch (e.keyCode)
    {
	case 13: /* CR */
	    e.line = e.target.value;
	    e.target.value = "";
	    if (e.line.search(/\S/) == -1)
		return;
	    jazzila.sendMessage(e);
	    break;
    }
}

function getDiv(text){
	var div = document.createElement ("html:span");
	div.appendChild(document.createTextNode(text));
	return div;
}

function getTableRow(jid, msg, dt){
	var cell = document.createElement("html:td");
	cell.appendChild(getDiv(getTime(dt))).setAttribute("class", "time");
	cell.appendChild(jid);
	cell.appendChild(msg);
	var row = document.createElement("html:tr");
	row.appendChild(cell);
	return(row);
}

function fillHtml(elem)
{
	var jid = getDiv("<"+getRDFLiteral(ds, elem, this.rsJid)+">");
	jid.setAttribute("class", getRDFLiteral(ds, elem, this.rsStyle));
	var msg = getDiv(getRDFLiteral(ds, elem, this.rsMsg));
	msg.setAttribute("class", "body");
	return getTableRow(jid, msg, getRDFLiteral(ds, elem, this.rsTimestamp));
}

function Render (box)
{
    this.rdf = CC["@mozilla.org/rdf/rdf-service;1"].getService(CI.nsIRDFService);
    this.cu = CC["@mozilla.org/rdf/container-utils;1"].getService(CI.nsIRDFContainerUtils); 
    this.rsTimestamp = this.rdf.GetResource("http://xmpp.org/RDF#timestamp");
    this.rsJid = this.rdf.GetResource("http://xmpp.org/RDF#jid");
    this.rsMsg = this.rdf.GetResource("http://xmpp.org/RDF#msg");
    this.rsStyle = this.rdf.GetResource("http://xmpp.org/RDF#style");
    this.box = box;
}

function fillXul(elem)
{
    var message = doc.createElement("message");
    message.setAttribute("from", "<" +  getRDFLiteral(ds, elem, this.rsJid)+"> ");
    message.setAttribute("time", getTime(getRDFLiteral(ds, elem, this.rsTimestamp)));
    message.setAttribute("class", getRDFLiteral(ds, elem, this.rsStyle));
    message.appendChild(this.doc.createTextNode(getRDFLiteral(ds, elem, this.rsMsg)));
    return message;
}

Render.prototype.render = function fillChat()
{
    var urn = this.rdf.GetResource("urn:xmpp:chat:"+window.name);
    var ctn = this.cu.MakeSeq(ds, urn);
    var etor = ctn.GetElements();
    while (etor.hasMoreElements())
    {
		var elem = etor.getNext().QueryInterface(CI.nsIRDFResource);
		this.box.appendChild(this.paint(elem));
    }
}

Render.prototype.paint = fillHtml;

function onChatLoad()
{
    ds  = jazzila.getDataSource();
    var frame = document.getElementById("msgframe");
    if (frame) 
    {
    window.removeEventListener("load", onChatLoad, true);
	var box = frame.contentDocument.getElementById("output-box");
	if (box)
	{
		render = new Render(box);
		render.render();
		jTL = new TreeListener();
		jTL.onAssert = AddMessage;
		jTL.onUnassert = ClearWindow;
		ds.AddObserver(jTL);
		setFocus();
	}
	frame.contentWindow.scrollTo(0, frame.contentDocument.height);
    }
    setFocus();
}

function onChatUnload()
{
    if (jTL)
	   ds.RemoveObserver(jTL);
}

function setFocus()
{
	var say = document.getElementById("say");
	if (say)
		say.focus();
}

var ds=null;
var jTL = null;
var render = null;
window.addEventListener("load", onChatLoad, true);
window.addEventListener("unload", onChatUnload, true);
